package com.liaoban.app.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * <p>圆角图片<p/>
 * Created by ybc on 16/8/16.
 */
public class RoundedImageFilletView extends AppCompatImageView {
    /*圆角的半径，依次为左上角xy半径，右上角，右下角，左下角*/
    private float[] rids = {15.0f,15.0f,15.0f,15.0f,15.0f,15.0f,15.0f,15.0f};

    public RoundedImageFilletView(Context context) {
        super(context);
    }

    public RoundedImageFilletView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoundedImageFilletView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void onDraw(Canvas canvas) {
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG| Paint.FILTER_BITMAP_FLAG));
        Path path = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
        /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/
        path.addRoundRect(new RectF(0,0,w,h),rids, Path.Direction.CW);
        try {
            canvas.clipPath(path);
        }catch (Exception e) {
        }

        super.onDraw(canvas);
    }

}
